package com.cy.pj.sys.service.impl;

import com.cy.pj.common.vo.Node;
import com.cy.pj.sys.dao.SysMenuDao;
import com.cy.pj.sys.entity.SysMenu;
import com.cy.pj.sys.service.SysMenuService;
import com.cy.pj.sys.service.exception.ServiceException;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 逗逼子民
 * @Date: 2019/7/10 15:02
 * @Description: @Slf4j会创建一个logger log;
 */
@Slf4j
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Autowired
    private SysMenuDao sysMenuDao;

    @Override
    public int updateObject(SysMenu entity) {
        //1.验证
        if (entity == null) {
            throw new ServiceException("保存对象不能为空");
        }
        if (StringUtils.isEmpty(entity.getName())) {
            throw new ServiceException("菜单名不能为空");
        }
        //2.更新数据
        int rows = sysMenuDao.updateObject(entity);
        if (rows == 0) {
            throw new ServiceException("记录可能已经不存在");
        }
        //3.返回数据
        return rows;
    }

    @Override
    public int saveObject(SysMenu entity) {
        int rows;
        //1.验证参数有效性
        if (entity == null) {
            throw new IllegalArgumentException("");
        }
        if (StringUtils.isEmpty(entity.getName())) {
            throw new IllegalArgumentException();
        }
        //2.持久化数据到数据库
        try {
            rows = sysMenuDao.insertObject(entity);
        } catch (Throwable e) {
            log.error(e.getMessage());
            throw new ServiceException(e.getMessage());
        }
        return 0;
    }

    @Override
    public List<Node> findZtreeMenuNodes() {
        return sysMenuDao.findZtreeMenuNodes();
    }

    @Override
    public List<Map<String, Object>> findObjects() {
        List<Map<String,Object>> list=sysMenuDao.findObjects();
        if(list==null||list.size()==0) {
            throw new ServiceException("没有对应数据");
        }
        return list;
    }
}
